package com.shimao.celnet.payment.controller.apinotcheck;


import com.shimao.celnet.payment.model.PPay;
import com.shimao.celnet.payment.model.PPayCust;
import com.shimao.celnet.payment.service.PPayCustService;
import com.shimao.celnet.payment.service.PPayService;
import com.shimao.celnet.payment.service.impl.apiservice.CallOut_TLPublicService;
import com.shimao.celnet.payment.util.MyDateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * 世茂支付平台 → 通联支付平台；   【数据】：支付结果；
 * 用于查询账单状态
 *  2017/12/14
 * christ
 */
@RestController
@RequestMapping(value = "/payQ")
public class CallOut_allinPaySelOutController {
    @Autowired
    private PPayService pPayService;

    @Autowired
    private PPayCustService pPayCustService;

    @Autowired
    private CallOut_TLPublicService callOut_tlPublicService;

    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public String allinPaySelOutController(@RequestParam(value = "paymentId") String Guid)  throws Exception{
       System.out.println("===========paymentId================="+Guid);
        Map<String, String> map = new TreeMap<String,String>();
        PPay pPayOrder = pPayService.getByKey(Guid);
        //根据小区sfid获取商户信息
        PPayCust pPayCust = getTLMessage(pPayOrder.getEstateGuid(),pPayOrder.getPayType());
        System.out.println("===========pPayOrder================="+pPayOrder);
        String reqsn = String.valueOf(System.currentTimeMillis());//随机时间戳
        int i = 0;
        /**
         * 前端使用ajax请求，4秒请求一次，共15次
         */
        if (pPayOrder != null) {
            System.out.println("===========coming=================");
            if("A03".equals(pPayOrder.getPayType())){ //快捷支付
                while (i < 15) {
                    String fail = "";
                    map = callOut_tlPublicService.queryCUP(MyDateUtil.NotString(pPayOrder.getGuid()), pPayOrder.getTrxid(),pPayCust);
                    if (map != null) {
                        for (String key : map.keySet()) {
                            if ("errmsg".equals(key)) {
                                fail = map.get(key);
                            }
                        }
                    }
                    if ("SUCCESS".equalsIgnoreCase(map.get("retcode"))&&"0000".equals(map.get("trxstatus")) && fail == "") {
                        return map.get("trxstatus");
                    } else {
                        Thread.sleep(4000);
                        i++;
                    }
                }


            }else {  //微信 支付宝的查询
                while (i < 15) {
                    String fail = "";
                    map = callOut_tlPublicService.query(reqsn, pPayOrder.getTrxid(),pPayCust);
                    if (map != null) {
                        for (String key : map.keySet()) {
                            if ("errmsg".equals(key)) {
                                fail = map.get(key);
                            }
                        }
                    }
                    if ("SUCCESS".equalsIgnoreCase(map.get("retcode"))&&"0000".equals(map.get("trxstatus")) && fail == "") {
                        return map.get("trxstatus");
                    } else {
                        Thread.sleep(4000);
                        i++;
                    }
                }
            }
        }
        return "00000000";
    }

    /**
     * 根据小区sfid查询通联账户信息
     * @param estateGuid
     * @return
     */
    public PPayCust getTLMessage(String estateGuid ,String payType){
        Map<String,String> pMap = new HashMap<String,String>();
        pMap.put("estateGuid",estateGuid);
        pMap.put("payType",payType);
        return pPayCustService.getBySfGuid(pMap);
    }
}
